package com.siblings.loan.ui.user.act

import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.lib.base.base.BaseActivity
import com.siblings.loan.R
import com.siblings.loan.databinding.ActMessageBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.ui.user.adapter.SibMessageAdapter
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventType
import org.json.JSONObject
import kotlin.random.Random

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/18 18:12
 * @Annotation：消息列表
 */
class SibMessageAct: BaseActivity() {
    lateinit var binding: ActMessageBinding

    private var messageAdapter: SibMessageAdapter? = null
    override fun getSibRootView(): View {
        binding = ActMessageBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarVisibility(View.VISIBLE)
        setSibBackBtnRes(R.mipmap.pic_back_theme)
        setSibRightBtnRes(R.mipmap.pic_kefu_theme)
        setSibTextTitle(getString(R.string.message_title))

        initSibRefreshLayout()
        initSibRecyclerView()
    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Message Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER,"Message Page")
        }
    }

    override fun initSibData() {
        val list = arrayListOf<JSONObject>()
        for (i in 0..Random.nextInt(20)){
            list.add(JSONObject())
        }
        messageAdapter!!.data = list
    }

    /* 初始化刷新 */
    private fun initSibRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            Handler(Looper.getMainLooper()).postDelayed({
                val list = arrayListOf<JSONObject>()
                for (i in 0..Random.nextInt(20)){
                    list.add(JSONObject())
                }
                messageAdapter!!.data = list
                binding.refreshLayout.finishRefresh()
            },1500)

        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化消息列表
     */
    private fun initSibRecyclerView() {
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
//        val topDecoration = DensityUtils.dp2px(12f)
//        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,topDecoration,topDecoration))
        messageAdapter = SibMessageAdapter(binding.recyclerView)
        binding.recyclerView.adapter = messageAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        messageAdapter!!.setOnItemChildClickListener { _, childView, position ->
            val item = messageAdapter!!.getItem(position)

        }
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_MESSAGE_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_MESSAGE_END,"")
    }
}